package com.xjrsoft.module.customerTwo.EMP.electricity_cost_records_month.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;

import java.io.File;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.xjrsoft.common.result.Response;
import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.CollectionUtil;
import com.xjrsoft.core.tool.utils.ExcelExportUtil;
import com.xjrsoft.module.customerTwo.EMP.electricity_cost_records_day.service.ITElectricityCostRecordsDayService;
import com.xjrsoft.module.customerTwo.EMP.electricity_cost_records_month.dto.SaveTElectricityCostRecordsMonthFormDataDto;
import com.xjrsoft.module.customerTwo.EMP.electricity_cost_records_month.dto.TElectricityCostRecordsMonthDto;
import com.xjrsoft.module.customerTwo.EMP.electricity_cost_records_month.vo.TElectricityCostRecordsMonthVo;
import com.xjrsoft.module.customerTwo.EMP.electricity_cost_records_month.vo.TElectricityCostRecordsMonthListVo;
import com.xjrsoft.module.customerTwo.EMP.electricity_cost_records_month.dto.TElectricityCostRecordsMonthListDto;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.FlowableUtil;
import com.xjrsoft.module.customerTwo.EMP.price_setting.service.ITPriceSettingService;
import com.xjrsoft.module.customerTwo.EMP.t_electric_meter_sum.service.ITElectricMeterSumService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customerTwo.EMP.electricity_cost_records_month.entity.TElectricityCostRecordsMonth;
import com.xjrsoft.module.customerTwo.EMP.electricity_cost_records_month.vo.TElectricityCostRecordsMonthVo;
import com.xjrsoft.module.customerTwo.EMP.electricity_cost_records_month.service.ITElectricityCostRecordsMonthService;

/**
 *  控制器
 *
 * @author dongxinhang
 * @since 2023-07-24
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/electricity_cost_records_month")
@Api(value = "月度电费记录", tags = "月度电费记录接口")
public class TElectricityCostRecordsMonthController {

	//每月电量记录
	private final ITElectricityCostRecordsMonthService tElectricityCostRecordsMonthService;
	//电量汇总
	private final ITElectricMeterSumService itElectricMeterSumService;
	//每日电量记录
	private final ITElectricityCostRecordsDayService itElectricityCostRecordsDayService;
	//电费单价设置
	private final ITPriceSettingService itPriceSettingService;


	/**
	 * 详情 
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入tElectricityCostRecordsMonth")
	@SaCheckPermission(value = {"electricity_cost_records_month:view", "electricity_cost_records_month:modify"}, mode = SaMode.OR)
	public Response<TElectricityCostRecordsMonthVo> getFormData(@PathVariable String id) {
		// 主表数据
		TElectricityCostRecordsMonth tElectricityCostRecordsMonth = tElectricityCostRecordsMonthService.getById(id);
		TElectricityCostRecordsMonthVo tElectricityCostRecordsMonthVo = BeanUtil.copy(tElectricityCostRecordsMonth, TElectricityCostRecordsMonthVo.class);
		return Response.ok(tElectricityCostRecordsMonthVo);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入tElectricityCostRecordsMonth")
		public Response<PageOutput<TElectricityCostRecordsMonthListVo>> getPageList(TElectricityCostRecordsMonthListDto listDto) {
		IPage<TElectricityCostRecordsMonth> page = tElectricityCostRecordsMonthService.getPageList(listDto);
		List<TElectricityCostRecordsMonthListVo> records = BeanUtil.copyList(page.getRecords(), TElectricityCostRecordsMonthListVo.class);
			// 工作流数据处理
		FlowableUtil.systemFormList(records, SecureUtil.getModuleId());
		// 转换列表数据
		DataTransUtil.transListShowData(records);
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 新增 
	 */
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入tElectricityCostRecordsMonth")
	@SaCheckPermission("electricity_cost_records_month:add")
	public Response save(@RequestBody TElectricityCostRecordsMonthDto formDto) {
		//获取电费单价
		BigDecimal electricityPrice = itPriceSettingService.getElectricityPrice();
		int month = Integer.valueOf(formDto.getMonth())+1;
		//处理起始日期
		String start = formDto.getYear()+"-"+formDto.getMonth()+"-01";
//		DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//		LocalDate dateParam = LocalDate.parse(start, df);
//		String end = dateParam.with(TemporalAdjusters.lastDayOfMonth()).toString();
		String end = "";
		if(month == 13){
			int year = Integer.valueOf(formDto.getYear())+1;
			end = year+"-01-01";
		}else {
			end = month <= 9 ? formDto.getYear()+"-0"+month+"-01" : formDto.getYear()+"-"+month+"-01";
		}
		//处理电费汇总
		boolean flag = itElectricMeterSumService.dealData(start,end);
		if(!flag){
			log.error("electricity_cost_records_month 处理电费汇总失败，日期范围 {}-{}",start,end);
			return Response.notOk("处理电费汇总失败,请联系管理员！");
		}
		//处理每日电费记录
		flag = itElectricityCostRecordsDayService.dealElectricityCostRecordsDay(start,end,electricityPrice);
		if(!flag){
			log.error("electricity_cost_records_month 处理每日电费记录失败，日期范围 {}-{}",start,end);
			return Response.notOk("处理每日电费记录失败,请联系管理员！");
		}
		//处理月电费记录
		int count = tElectricityCostRecordsMonthService.insertDataByDay(start,end);
		log.info("electricity_cost_records_month 费用结算完毕，共处理{}条记录",count);
		return Response.ok();
	}


	@GetMapping("/export-file")
	@ApiOperation(value = "导出excel文件")
	public Response exportExcel(TElectricityCostRecordsMonthListDto listDto) {
		List<Map<String,Object>> dataList = tElectricityCostRecordsMonthService.exportExcel(listDto);
		return Response.ok(dataList);
	}

	/**
	 * 自定义分页
	 */
	@GetMapping("/queryList")
	@ApiOperation(value = "分页", notes = "传入tElectricityCostRecordsMonth")
	public Response queryList(TElectricityCostRecordsMonthListDto listDto){
		IPage<Map<String,Object>> dataList = tElectricityCostRecordsMonthService.queryPage(new Page<>(listDto.getLimit(), listDto.getSize()),listDto);
		return Response.ok(dataList);
	}
}
